<?php
	
	// Session
	session_start();
	
	
	// Nese leximi i faqes behet nga ndonje burim ekstern duke mos e definuar identifikatorin e librit
	if (!isset($_GET['id'])) {
		header("Location: ../comunity/index.php?spalsh=wrong");
		exit;
	}
	
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	include('../include/book.class.php');
	
	
	// PROJECT
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/comunity/share.html');
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	define('UID',$_SESSION['user_id']);
	define('ID',$_GET['id']);
	$getBook = "?id=".ID;
	
	
	// DB CONNECT
	dbOpen("comunity");	
	
	
	// TMP CLASS
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// SPLASH SCREEN
	if (isset($_GET['splash'])) 
		splash($_GET['splash'], "../comunity/share.php$getBook");
	
	
	// BOOK CLASS
	$book = new Book(ID);
	$book->setBook();
		
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../comunity"));
	$tmp->setContent("BOOKMENU",$tmp->menu("book",FILENAME,$getBook));
	$tmp->setContent("SECTIONS",sections($book->BOOK['sid']));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	

	// ACCORDIONS
	$tmp->setContent("INFOBOOK",$book->BOOK);
	$tmp->setContent("FROMAUTHOR",$book->fromAuthor());
	$tmp->setContent("READMORE",$book->readMore());
	$tmp->setContent("NEWS",news("comunity"));
	$tmp->setContent("RANDOM",randomContent("comunity"));
	
	
	// AUTHOR
	$book->BOOK['db'] = "comunity";
	$tmp->setContent($book->BOOK);
	
	// PDF Text
	$tmp->setContent($tmp->xmlContent('share',"help.xml"));
	
	
	if (isset($_POST['sharemail'])) {
		
		if (empty($_POST['sharename']))
			$ERROR['sharename'] = "null";
			
		if (empty($_POST['sharemail']))
			$ERROR['sharemail'] = "null";
		elseif(!ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $_POST['sharemail']))
			$ERROR['sharemail'] = "syntax";

		// Nese nuk ka asnje gabim
		if(empty($ERROR)) {
			
			// MAIL TEMPLATE
				$tmpMail = new Template("../include/styles/default/sendmail/share.html",$PROJECT['lang']);
				
				// XML INCLUDING
				$path = "../include/lang/".$PROJECT['lang']."/mail.xml";
				$xmlMail = simplexml_load_file($path);
				
				// Ndyshoret per zevendesim ne HTML shabllon
				$USER['subject'] = html_encrypt($xmlMail->share->subject);
				$USER['noreplay'] = nl2br($xmlMail->noreplay->body);
				$USER['content'] = nl2br($tmpMail->xmlLink($xmlMail->share->body));
				$USER['senddate'] = setDate();
				
				// Konverto ndryshoret ne HTML shabllon
				$tmpMail->setContent($USER);
				
				// Ndyshoret per zevendesim ne XML shabllon
				$VARS['author'] = $book->BOOK['author'];
				$VARS['title'] = "<a href='http://www.dielli.net/comunity/preface.php?id=".ID."'><b>".$book->BOOK['volume']."</b> ".$book->BOOK['title']."</a>";
				$VARS['user'] = $_SESSION['user_name'];
				$VARS['sharename'] = $_POST['sharename'];
				
				// Konverto ndryshoret ne shabllonin e Acountit
				$tmpMail->setXMLVars($VARS);
				
				// Konverto emrtimet ne shabllon
				$tmpMail->setLabels();
				
				// HEADERS				
				$header = 'MIME-Version: 1.0' . "\r\n";
				$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";					
				$header .= "From: ".$PROJECT['name']." <".$PROJECT['noreplay'].">\r\n";
				
				// SEND MAIL
				mail($_POST['sharemail'], $USER['subject'], $tmpMail->vorlage, $header);
				
				// Rilexo faqen me porosin per sukses
				header("Location: share.php?id=".ID."splash=share"); 
				exit;
		}
	}
	
	// LABELS
	$tmp->setLabels();
	
	
	// Convert POST and ERROR vars in Form
	$tmp->setForm($_POST,$ERROR);
	
	
	// DISPLAY ALL						  
	echo $tmp->vorlage;
?>